package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.controller.ArticleCrawlerController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by wangyong on 2017/12/4.
 */
@Service
public class YiBangDongLiCrawler extends BaseService {


    @Autowired
    private ArticleCrawlerController articleCrawlerController;


    public JsonResult crawler() {
        String json = WebUtil.buildRequest("http://www.ebrun.com/rss/original.xml", true);
        String[] strings = json.split("</link>");
        String link;
        JsonResult jsonResult;
        int success = 0;
        int ero = 0;
        for (String s : strings) {
            link = s.substring(s.indexOf("<link>") + 6);
            if (!link.contains(".shtml")) {
                continue;
            }
            jsonResult = articleCrawlerController.crawl(6421, link);
            if (jsonResult.getStatus() == JsonResult.STATUS_SUCCESS) {
                success++;
                ero = 0;
            } else {
                if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
                    ero++;
                    if (ero > 10) {
                        return error("连续出错10次，请核实问题");
                    }
                }
            }
        }
        return success("亿邦动力网文章导入成功，累计" + success + "篇");
    }
}
